//
//  TapAuthorImageTableViewController.m
//  bh
//
//  Created by lanou on 15/12/24.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "TapAuthorImageTableViewController.h"
#import "FirstDatailTableViewCell.h"
#import "TapAuthorView.h"
#import "HaveFourPicCollectionViewCell.h"

#import "Photos.h"


@interface TapAuthorImageTableViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)Photos *photos;

@property (nonatomic,strong)NSMutableArray *photoArray;
@property (nonatomic,strong)TapAuthorView *hearderView;


@end

@implementation TapAuthorImageTableViewController

- (NSMutableArray *)photoArray{
    if (_photoArray == nil) {
        _photoArray = [[NSMutableArray alloc]init];
    }
    return _photoArray;
}



- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController setNavigationBarHidden:YES];
}


- (void)jsonWithTapImageDetail{
    NSString *url = [NSString stringWithFormat:kSecondTapImageDetailUrl,self.authorID];
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dict = (NSDictionary *)response;
        NSDictionary *dic1 = dict[@"data"][@"user_home_ideabook_list"];

        //collection的解析
        
        for (NSDictionary *dic2 in dic1[@"collections"]) {
            Photos *photos = [Photos shareWithDictionary:dic2];
            [self.photoArray addObject:photos];
        }
        
        NSDictionary *user_info = dict[@"data"][@"user_home_info"][@"user_info"];
        self.hearderView.photos = nil;
       self.hearderView.photos = [Photos shareWithDictionary:user_info];

        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self jsonWithTapImageDetail];
    
    self.hearderView =[[TapAuthorView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 250))];
    
    
    self.tableView.tableHeaderView = self.hearderView;
    [self.hearderView.backButton addTarget:self action:@selector(backLastView) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.tableView registerClass:[FirstDatailTableViewCell class] forCellReuseIdentifier:@"cell1"];
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//返回到上一个界面
- (void)backLastView{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    FirstDatailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
    cell.FirstCollectionView.delegate = self;
    cell.FirstCollectionView.dataSource =self;
    [cell.FirstCollectionView registerClass:[HaveFourPicCollectionViewCell class] forCellWithReuseIdentifier:@"cell2"];
    [cell.FirstCollectionView reloadData];
    
    // Configure the cell...
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *firstView =[[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 30))];
    UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(5, 20, 100, 30))];
    label.text = @"灵感画册";
    firstView.backgroundColor = [UIColor cyanColor];
    label.textColor = [UIColor orangeColor];
       [firstView addSubview:label];
    return firstView;

    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 600;
}

#pragma mark -----collectionView的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.photoArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HaveFourPicCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
    cell.photos = self.photoArray[indexPath.item];

    return cell;
    
}






/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
